package com.company.example.service.impl;

import com.company.example.core.common.DaoHelperTest;
import com.company.example.service.BaseService;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

@Service
public class BaseServiceImpl<T,PK extends Serializable> implements BaseService<T, PK>{
	//private final static String SPACE = "@TestMapper";
	private String nameSpace = "";

	public BaseServiceImpl() {
        Class<?> c = getClass();
        String name = c.getSimpleName();
		nameSpace = name.substring(0, name.indexOf("Service")) + "TestMapper";
    }

	public String getNameSpace() {
		return nameSpace;
	}

	@Override
	public boolean insert(T t) {
		return DaoHelperTest.insert(nameSpace, "insert", t);
	}

	@Override
	public boolean delete(PK pk) {
		return DaoHelperTest.delete(nameSpace, "delete", pk);
	}

	@Override
	public boolean update(T t) {
		return DaoHelperTest.update(nameSpace, "update", t);
	}

	@SuppressWarnings("unchecked")
	@Override
	public T selectByPrimaryKey(PK pk) {
		return (T) DaoHelperTest.query(nameSpace, "selectByPrimaryKey", pk);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<T> selectAllObjects() {
		return (List<T>) DaoHelperTest.list(nameSpace, "selectAllObjects", null);
	}

	@Override
	public int selectTotalRows() {
		return DaoHelperTest.count(nameSpace,"selectTotalRows",null);
	}
}
